package com.yk.framework.icons;

import java.util.HashMap;

import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 * @author lifetime
 *
 */
public abstract class IconReader {
	static HashMap<String, Icon> iconCache = new HashMap<String, Icon>();

	public HashMap<String, Icon> getIconCache() {
		return iconCache;
	}
	
	public void flush(){
		iconCache = new HashMap<String, Icon>();
	}

	public static Icon getIcon(String iconName) {
		if(iconName == null || iconName.trim().length() == 0){
			return null;
		}
		ImageIcon icon = null;
		if (iconCache.containsKey(iconName)) {
			icon = (ImageIcon)iconCache.get(iconName);
			if (icon == null) {
				icon = new ImageIcon(IconReader.class.getResource(iconName));
				icon.setDescription(iconName);
				iconCache.put(iconName, icon);
			}
		} else {
			icon = new ImageIcon(IconReader.class.getResource(iconName));
			icon.setDescription(iconName);
			iconCache.put(iconName, icon);
		}
		return icon;
	}

	public static void main(String[] args) {
		System.out.println(IconReader.getIcon("wf_end_complete.png"));
	}
}
